package com.codi.api.controller;

import com.codi.base.domain.*;
import com.codi.base.exception.BaseAppException;
import com.codi.base.util.Assert;
import com.codi.superman.base.annotation.RequiresPermissions;
import com.codi.superman.base.domain.SysBanner;
import com.codi.superman.base.service.SysBannerService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 周翔 on 2017/4/5.
 */
@RestController
@RequestMapping("/home/banner")
public class HomeBannerController extends BaseController {
    @Autowired
    private SysBannerService sysBannerService;

    /**
     * 用户所有的banner信息
     *
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value = "banners", method = RequestMethod.GET)
    public BaseResult getBannersUser() throws BaseAppException {
        List<SysBanner> list = sysBannerService.queryBannersUser();
        List<SysBanner> listRest = Lists.newArrayList();
        for(SysBanner banner:list){
            if(banner.getName().endsWith("H5") || banner.getName().endsWith("ORG")){
                continue;
            }
            listRest.add(banner);
        }
        return BaseResult.success(listRest);
    }

    /**
     * 管理员所有的banner信息
     *
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value = "bannersAdmin", method = RequestMethod.GET)
    @RequiresPermissions("sys:banner:query")
    public BaseResult getBannersAdmin() throws BaseAppException {

        return BaseResult.success(sysBannerService.queryBannersAdmin());
    }


    /**
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @RequiresPermissions("sys:banner:add")
    public BaseResult addBanner(SysBanner sysBanner) throws BaseAppException {
        Assert.notNull(sysBanner);
        sysBannerService.addBanner(sysBanner);
        return BaseResult.success(sysBanner);
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @RequiresPermissions("sys:banner:delete")
    public BaseResult deleteBanner(@RequestParam(value = "id") Long id) throws BaseAppException {
        sysBannerService.deleteBanner(id);
        return BaseResult.success();
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    @RequiresPermissions("sys:banner:update")
    public BaseResult updateBanner(SysBanner sysBanner) throws BaseAppException {
        Assert.notNull(sysBanner);
        Assert.notNull(sysBanner.getId());
        sysBannerService.updateBanner(sysBanner);
        return BaseResult.success();
    }

}
